﻿<%@ Page Title="Tra cứu chuyên gia" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Dost.Khcn.Web.Admin._Default" %>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <h3>Tra cứu chuyên gia</h3>
    <div class="filtering">
        <table>
            <tr>
                <td>Tên: </td>
                <td>
                    <input data-bind="value: searchModel().hoTen" class="name" />
                </td>
            </tr>
            <tr>
                <td>Địa điểm công tác: </td>
                <td>
                    <input data-bind="value: searchModel().tenDonVi" class="name" />
                </td>
            </tr>
            <tr>
                <td>Địa chỉ công tác: </td>
                <td>
                    <input data-bind="value: searchModel().diaChiDonVi" class="name" /></td>
            </tr>
            <tr>
                <td>Lĩnh vực nghiên cứu: </td>
                <td>
                    <select data-bind="options: $root.NhomLinhVucs, optionsText: 'MaTen', optionsValue: 'Id', value: searchModel().nhomLinhVucId" class="name"></select>
                </td>
            </tr>
            <tr>
                <td>Ngành nghiên cứu: </td>
                <td>
                    <select data-bind="options: linhVucs, optionsText: 'MaTen', optionsValue: 'Id', value: searchModel().linhVucId" class="name"></select>
                </td>
            </tr>
            <%if (IsAdvanceSearch)
              { %>
            <tr>
                <td>Tên luận án:</td>
                <td>
                    <input data-bind="value: searchModel().tenLuanAn" class="name" /></td>
            </tr>
            <tr>
                <td>Công trình nghiên cứu:</td>
                <td>
                    <input data-bind="value: searchModel().tenCongTrinh" class="name" /></td>
            </tr>
            <% } %>
            <tr>
                <td></td>
                <td>
                    <button data-bind="click: search">Tra cứu</button>
                    <button data-bind="click: reset">Làm mới</button></td>
            </tr>
        </table>

    </div>
    <div id="ListContainer">
    </div>

    <script type="text/javascript">
        var advanceSearch = <%=IsAdvanceSearch?"true":"false"%>;
        if (advanceSearch) {
            $(function() {
                $('#ListContainer').jtable({
                    //title: 'Chuyên gia',
                    paging: true,
                    pageSize: 10,
                    sorting: true,
                    defaultSorting: 'HoTen ASC',
                    actions: {
                        listAction: 'Default.aspx/SearchChuyenGiaAdvance'
                    },
                    fields: {
                        HoTen: {
                            title: 'Họ tên',
                            display: function(data) {
                                return '<a href="ChuyenGiaDetails.aspx?id=' + data.record.Id + '">' + data.record.HoTen + '</a>';
                            }
                        },
                        TenDonVi: {
                            title: 'Địa điểm công tác',
                            sorting: false
                        },
                        DiaChiDonVi: {
                            title: 'Địa chỉ',
                            sorting: false
                        },
                        TenLinhVuc: {
                            title: 'Lĩnh vực nghiên cứu',
                            sorting: false
                        },
                        TenLuanAn: {
                            title: 'Tên luận án',
                            sorting: false
                        },
                        CongTrinh: {
                            title: 'Công trình nghiên cứu',
                            sorting: false,
                            width: '30%'
                        }
                    }
                });

                ko.applyBindings(new SearchViewModel());
            });
        } else {
            $(function() {
                $('#ListContainer').jtable({
                    //title: 'Chuyên gia',
                    paging: true,
                    pageSize: 10,
                    sorting: true,
                    defaultSorting: 'HoTen ASC',
                    actions: {
                        listAction: 'Default.aspx/SearchChuyenGiaSimple'
                    },
                    fields: {
                        HoTen: {
                            title: 'Họ tên',
                            width: '25%'
                            //display: function(data) {
                            //    return '<a href="ChuyenGiaDetails.aspx?id=' + data.record.Id + '">' + data.record.HoTen + '</a>';
                            //}
                        },
                        TenDonVi: {
                            title: 'Địa điểm công tác',
                            width: '25%',
                            sorting: false
                        },
                        DiaChiDonVi: {
                            title: 'Địa chỉ',
                            width: '25%',
                            sorting: false
                        },
                        TenLinhVuc: {
                            title: 'Lĩnh vực nghiên cứu',
                            width: '25%',
                            sorting: false
                        }
                    }
                });

                ko.applyBindings(new SearchViewModel());
            });
        }
    </script>

    <script type="text/javascript">
        var SearchModel = {
            hoTen: ko.observable(""),
            tenDonVi: ko.observable(""),
            diaChiDonVi: ko.observable(""),
            nhomLinhVucId: ko.observable(0),
            linhVucId: ko.observable(0)
        };

        var AdvanceSearchModel = {
            hoTen: ko.observable(""),
            tenDonVi: ko.observable(""),
            diaChiDonVi: ko.observable(""),
            nhomLinhVucId: ko.observable(0),
            linhVucId: ko.observable(0),
            tenLuanAn: ko.observable(""),
            tenCongTrinh: ko.observable("")
        };

        function SearchViewModel() {
            var self = this;

            // data
            if (advanceSearch) {
                self.searchModel = ko.observable(AdvanceSearchModel);
            } else {
                self.searchModel = ko.observable(SearchModel);
            }

            // nhom linh vuc
            self.NhomLinhVucs = ko.observableArray();
            $.ajax({
                url: "Default.aspx/GetAllNhomLinhVucs",
                type: "post",
                contentType: "application/json",
                async: false,
                success: function (data) {
                    self.NhomLinhVucs(data.d);
                }
            });

            // linh vuc
            self.allLinhVucs = {};
            self.linhVucs = ko.observableArray();
            $.ajax({
                url: "Default.aspx/GetAllLinhVucs",
                type: "post",
                contentType: "application/json",
                async: true,
                success: function (data) {
                    self.allLinhVucs = data.d;
                    self.linhVucs(data.d);
                }
            });

            // behaviors
            self.searchModel().nhomLinhVucId.subscribe(function () {
                if (self.searchModel().nhomLinhVucId() === 0) {
                    self.linhVucs(self.allLinhVucs);
                } else {
                    var linhVucs = new Array();
                    $.each(self.allLinhVucs, function (index, linhVuc) {
                        if (linhVuc.Id === 0) {
                            linhVucs.push(linhVuc);
                        }
                        else
                            if (linhVuc.NhomLinhVucId === self.searchModel().nhomLinhVucId()) {
                                linhVucs.push(linhVuc);
                            }
                    });
                    self.linhVucs(linhVucs);
                }
            });

            // events
            self.search = function () {
                $('#ListContainer').jtable('load', { searchModel: ko.toJS(self.searchModel) });
            };

            self.reset = function () {
                self.searchModel().hoTen("");
                self.searchModel().tenDonVi("");
                self.searchModel().diaChiDonVi("");
                self.searchModel().nhomLinhVucId(0);
                self.searchModel().linhVucId(0);
                if (advanceSearch) {
                    self.searchModel().tenLuanAn("");
                    self.searchModel().tenCongTrinh("");
                }
                self.search();
            };

            //self.search();
        }
    </script>
</asp:Content>
